function TaskPool() {
	var _self = this;
	_self.storeId = "ForDamStorage";
	_self.tasks = null;

	var _loadTasks = function() {
		_self.tasks = window.localStorage.getItem(_self.storeId);
		if (!_self.tasks || _self.tasks.length == 0) {
			_self.tasks = [];
		} else {
			var temp = [];
			_self.tasks = jQuery.parseJSON(_self.tasks);
			for ( var i = 0; i < _self.tasks.length; i++) {
				temp.push(new TaskItem(_self.tasks[i]));
			}
			_self.tasks = temp;
		}
	};

	var _storeTasks = function() {
		window.localStorage.setItem(_self.storeId, JSON.stringify(_self.tasks));
	};

	var _isTodaytask = function(item) {
		var begin = new Date();
		begin.setHours(0);
		begin.setMinutes(0);
		begin.setSeconds(0);

		var end = new Date();
		end.setHours(23);
		end.setMinutes(59);
		end.setSeconds(59);

		var taskAt = new Date(item.getAlertTime());
		if ((taskAt.getTime() >= begin.getTime())
				&& (taskAt.getTime() < end.getTime())) {
			return true;
		}
		return false;
	};

	_self.initTaskPool = function() {
		_loadTasks();
	};
	
	_self.storeTaskpool = function(){
		_storeTasks();
	};

	_self.getAllTask = function() {
		return _self.tasks;
	};

	_self.getTodayTask = function() {
		if (_self.tasks.length == 0) {
			return _self.tasks;
		}

		var begin = new Date();
		begin.setHours(0);
		begin.setMinutes(0);
		begin.setSeconds(0);
		var end = new Date();
		end.setHours(23);
		end.setMinutes(59);
		end.setSeconds(59);

		var re = [];
		for ( var i = 0; i < _self.tasks.length; i++) {
			if (_isTodaytask(_self.tasks[i])) {
				re.push(_self.tasks[i]);
			}
		}
		return re;
	};

	_self.getPopTask = function() {
		var re = [];
		if (_self.tasks.length == 0) {
			return re;
		}
		var now = new Date();
		for ( var i = 0; i < _self.tasks.length; i++) {
			if (_self.tasks[i].getStatus() != 0 || (!_isTodaytask(_self.tasks[i]))) {
				continue;
			}
			var item = _self.tasks[i];
			var at = new Date(item.getAlertTime());
			if ((at.getTime() + 20 * 1000) < now.getTime()) {
				re.push(item);
			}
		}
		return re;
	};

	_self.addTask = function(taskItem) {
		var inserted = false;
		for ( var i = 0; i < _self.tasks.length; i++) {
			var item = _self.tasks[i];
			if (taskItem.getAlertTime() < item.getAlertTime()) {
				_self.tasks.splice(i, 0, taskItem);
			}
		}
		if (!inserted) {
			_self.tasks.push(taskItem);
		}
		_storeTasks();
	};

	_self.updateTask = function(taskId, status) {
		for ( var i = 0; i < _self.tasks.length; i++) {
			var item = _self.tasks[i];
			if (item.getTaskId() == taskId) {
				item.setStatus(status);
			}
		}
		_storeTasks();
	};

	_self.delTask = function(taskUUID) {
		for ( var i = 0; i < _self.tasks.length; i++) {
			if (_self.tasks[i].getTaskId() == taskId) {
				_self.tasks.splice(i, 1);
			}
		}
		_storeTasks();
	};

};